<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>主页</title>
    <link rel="stylesheet" href="../../vendors/mdi/css/materialdesignicons.min.css">
    <link rel="stylesheet" href="../../vendors/base/vendor.bundle.base.css">
    <link rel="stylesheet" href="../../vendors/datatables.net-bs4/dataTables.bootstrap4.css">
    <link rel="stylesheet" href="../../css/style.css">
    <link rel="shortcut icon" href="../../images/favicon.png" />
</head>
<style>
</style>

<body>
<div class="container-scroller">
    <!-- 头部内容 -->
    <nav class="navbar col-lg-12 col-12 p-0 fixed-top d-flex flex-row">
        <div class="navbar-brand-wrapper d-flex justify-content-center">
            <div class="navbar-brand-inner-wrapper d-flex justify-content-between align-items-center w-100">
                <a class="navbar-brand brand-logo" href="../../home.html"><img src="../../images/logo.svg" alt="logo" /></a>
                <a class="navbar-brand brand-logo-mini" href="../../home.html"><img src="../../images/logo-mini.svg" alt="logo" /></a>
                <button class="navbar-toggler navbar-toggler align-self-center" type="button" data-toggle="minimize">
                    <span class="mdi mdi-sort-variant"></span>
                </button>
            </div>
        </div>
        <div class="navbar-menu-wrapper d-flex align-items-center justify-content-end">
            <ul class="navbar-nav mr-lg-4 w-100">
                <li class="nav-item nav-search d-none d-lg-block w-100">
                    <div class="input-group">
                        <div class="input-group-prepend">
                <span class="input-group-text" id="search">
                  <i class="mdi mdi-magnify"></i>
                </span>
                        </div>
                        <input type="text" class="form-control" placeholder="Search now" aria-label="search"
                               aria-describedby="search">
                    </div>
                </li>
            </ul>
            <ul class="navbar-nav navbar-nav-right">
                <!-- 消息图标 -->
                <li class="nav-item dropdown me-1">
                    <a class="nav-link count-indicator dropdown-toggle d-flex justify-content-center align-items-center"
                       id="messageDropdown" href="#" data-bs-toggle="dropdown">
                        <i class="mdi mdi-message-text mx-0"></i>
                        <span class="count"></span>
                    </a>
                </li>
                <!-- 提醒图标 -->
                <li class="nav-item dropdown me-4">
                    <a class="nav-link count-indicator dropdown-toggle d-flex align-items-center justify-content-center notification-dropdown"
                       id="notificationDropdown" href="#" data-bs-toggle="dropdown">
                        <i class="mdi mdi-bell mx-0"></i>
                        <span class="count"></span>
                    </a>
                </li>
                <!-- 用户头像 -->
                <li class="nav-item nav-profile dropdown">
                    <a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown" id="profileDropdown">
                        <img src="../../images/faces/face5.jpg" alt="profile" />
                        <span class="nav-profile-name">草莓熊</span>
                    </a>
                    <div class="dropdown-menu dropdown-menu-right navbar-dropdown" aria-labelledby="profileDropdown">
                        <a class="dropdown-item">
                            <i class="mdi mdi-settings text-primary"></i>
                            设置
                        </a>
                        <a class="dropdown-item" href="../../login.html">
                            <i class="mdi mdi-logout text-primary"></i>
                            退出
                        </a>
                    </div>
                </li>
            </ul>
            <button class="navbar-toggler navbar-toggler-right d-lg-none align-self-center" type="button"
                    data-toggle="offcanvas">
                <span class="mdi mdi-menu"></span>
            </button>
        </div>
    </nav>
    <!-- 导航栏+内容部分 -->
    <div class="container-fluid page-body-wrapper">
        <!--导航栏-->
        <nav class="sidebar sidebar-offcanvas" id="sidebar">
            <ul class="nav">
                <li class="nav-item">
                    <a class="nav-link" href="../../home.html">
                        <i class="mdi mdi-home menu-icon"></i>
                        <span class="menu-title">主页</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="../article/article.html">
                        <i class="mdi mdi-view-headline menu-icon"></i>
                        <span class="menu-title">文章管理</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="../picture/picture.html">
                        <i class="mdi mdi-view-headline menu-icon"></i>
                        <span class="menu-title">图片管理</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">
                        <i class="mdi mdi-view-headline menu-icon"></i>
                        <span class="menu-title">视频管理</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="user.html">
                        <i class="mdi mdi-chart-pie menu-icon"></i>
                        <span class="menu-title">用户管理</span>
                    </a>
                </li>
            </ul>
        </nav>
        <!--内容主题部分-->
        <div class="main-panel">
            <div class="container-fluid">
                <div style="display: inline-block; margin-top:30px;">
                    <button type="button" class="btn btn-primary " data-toggle="modal" data-target="#myModal">添加用户</button>
                    <!--搜索框-->
                    <input type="text" id="ret" placeholder="搜索用户名" style="height: 40px; margin-left: 100px">
                    <!--搜索按钮-->
                    <button type="button" class="btn btn-success set_btn">搜索</button>
                </div>
                <table class="table table-bordered table-striped table-hover bg-color" style="width: 100%; height: auto; margin-top: 30px;">
                    <thead>
                    <tr>
                        <th>序号</th>
                        <th>账号</th>
                        <th>密码</th>
                        <th>级别</th>
                        <th style="width: 15%">操作</th>
                    </tr>
                    </thead>
                    <tbody>
<!--                    <tr>-->
<!--                        <td>1</td>-->
<!--                        <td>John</td>-->
<!--                        <td>Doe</td>-->
<!--                        <td>-->
<!--                            <button type="button" class="btn btn-success">修改</button>-->
<!--                            <button type="button" class="btn btn-danger">删除</button>-->
<!--                        </td>-->
<!--                    </tr>-->
                    </tbody>
                </table>
                <!--分页-->
<!--                <ul class="pagination" style="margin-top: 30px">-->
<!--                    <li class="page-item"><a class="page-link" href="#">上一页</a></li>-->
<!--                    <li class="page-item"><a class="page-link" href="#">1</a></li>-->
<!--                    <li class="page-item"><a class="page-link" href="#">2</a></li>-->
<!--                    <li class="page-item"><a class="page-link" href="#">3</a></li>-->
<!--                    <li class="page-item"><a class="page-link" href="#">下一页</a></li>-->
<!--                </ul>-->
                <!-- 模态框 -->
                <div class="modal fade" id="myModal">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <!-- 模态框头部 -->
                            <div class="modal-header">
                                <h4 class="modal-title">添加AND修改</h4>
                            </div>
                            <!-- 模态框主体 -->
                            <div class="modal-body">
                                <form>
                                    <!--用于判断是添加操作还是删除操作,把它隐藏起来  style="display: none;"-->
                                    <div class="form-group" style="display: none;">
                                        <label for="usr">id:</label>
                                        <input type="text" class="form-control" id="id">
                                    </div>
                                    <div class="form-group">
                                        <label for="usr">账号:</label>
                                        <input type="text" class="form-control" id="usr" placeholder="请输入账号">
                                    </div>
                                    <div class="form-group">
                                        <label for="pwd">密码:</label>
                                        <input type="text" class="form-control" id="pwd" placeholder="输入密码">
                                    </div>
                                    <div class="radio">
                                        <label class="radio-inline" style="margin-left: 30px"><input type="radio" name="opt-radio">普通用户</label>
                                        <label class="radio-inline" style="margin-left: 30px"><input type="radio" name="opt-radio">管理员</label>
                                    </div>
                                    <div style="margin-top: 30px">
                                        <button type="button" class="btn btn-primary submit">提交</button>
                                        <button type="button" class="btn btn-secondary close" data-dismiss="modal" style="float: right">关闭</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="../../vendors/base/vendor.bundle.base.js"></script>
<script src="../../vendors/chart.js/Chart.min.js"></script>
<script src="../../vendors/datatables.net/jquery.dataTables.js"></script>
<script src="../../vendors/datatables.net-bs4/dataTables.bootstrap4.js"></script>
<script src="../../js/off-canvas.js"></script>
<script src="../../js/hoverable-collapse.js"></script>
<script src="../../js/dashboard.js"></script>
<script src="../../js/data-table.js"></script>
<script src="../../js/jquery.dataTables.js"></script>
<script src="../../js/dataTables.bootstrap4.js"></script>
<script src="../../js/jquery.cookie.js" type="text/javascript"></script>
<script src="../../js/axios.js"></script>
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script>
    //页面加载完成后（不一定是刷新后）调用一个函数
    window.onload = function () {
        console.log('页面加载完成')
        //调用渲染页面函数
        getUserList()
        
    }

    //使用axios发起请求获取数据函数,渲染页面函数(局部刷新，不会有跳转，对于用户体验更加友好)
    function getUserList() {
        //发起axios请求获取数据
        let url = 'http://localhost:8080/my_demo/user'
        axios({
            method: "get",
            url: url,
        }).then(res => {
            if (res.status === 200){
                let data = res.data
                //调用render页面渲染函数,传入前端返回的数据
                render(data)
            }
        })
    }

    //使用axios发起请求传回需要删除用户的id
    function deleteUser(id) {
        let url = 'http://localhost:8080/my_demo/deleteUser'
        axios({
            method: "get",
            url: url,
            params:{
                id:id
            }
        }).then(res => {
            if (res.data.code === 200){
                //调用getUserList()函数重新渲染页面
                getUserList()
                alert(res.data.msg)
            } else {
                alert(res.data.msg)
            }
        })
    }

    //使用axios发起请求传回需要添加的用户信息
    function addUser(data) {
        //请求地址
        let url = 'http://localhost:8080/my_demo/register'
        //发送axios请求
        axios({
            method: "post",
            url: url,
            params: data
        }).then(res => {
            //将后端响应的数据转换
            if (res.data.code === 200){
                //清空输入框
                clearInput()
                alert("添加成功!")
                //调用getUserList(),重新查询数据库数据,并渲染到页面
                getUserList()
                //添加成功,关闭模态框
                let btn = document.querySelector('.close')
                btn.click()
            } else if (res.data.code === 304){
                //清空输入框
                clearInput()
                alert("添加失败,账号或密码不能为空!")
            } else {
                //清空输入框
                clearInput()
                alert("添加失败,账号已存在！")
            }
        })
    }


    //使用axios发起请求传回id获取需要修改的用户信息,填入输入框
    function getUserById(id) {
        //请求地址
        let url = `http://localhost:8080/my_demo/doUpdate?id=${id}`
        //发送axios请求
        axios({
            method: "get",
            url: url,
        }).then(res => {
            //将后端响应的信息放入输入框
            document.querySelector("#id").value = res.data.data.id
            document.querySelector("#usr").value = res.data.data.username
            document.querySelector("#pwd").value = res.data.data.password
        })
    }


    //使用axios发起请求传回需要修改的用户信息
    function updateUser(data) {
        //请求地址
        let url = 'http://localhost:8080/my_demo/update'
        //发送axios请求
        axios({
            method: "post",
            url: url,
            params: data
        }).then(res => {
            //将后端响应的数据转换
            if (res.data.code===200){
                alert(res.data.msg)
                //调用getUserList(),重新查询数据库数据,并渲染到页面
                getUserList()
                //添加成功,关闭模态框
                let btn = document.querySelector('.close')
                btn.click()
            }
        })
    }

    //点击关闭按钮清空输入框,先获取关闭按钮
    let btn = document.querySelector('.close')
    //点击关闭按钮
    btn.addEventListener('click',function () {
        //调用清空输入框函数
        clearInput()
    })
    
    //清空输入框函数
    function clearInput () {
        document.querySelector("#id").value = ''
        document.querySelector("#usr").value = ''
        document.querySelector("#pwd").value = ''
    }

    //渲染页面函数
    function render(data) {
        //1.首先定义一个空字符串，用于存储一会遍历的数据，再放会页面
        let str = ''
        //2.利用forEach方法遍历数组，把数据拼接成字符串
        data.forEach(function (item,index) {
            str += `
            <tr>
              <td>${index+1}</td>
              <td>${item.username}</td>
              <td>${item.password}</td>
              <td>${item.type}</td>
              <td>
                  <button type="button" class="btn btn-success" data-toggle="modal" data-target="#myModal" data-id=${item.id}>修改</button>
                  <button type="button" class="btn btn-danger" data-id=${item.id}>删除</button>
              </td>
            </tr>
            `
        })
        //3.获取页面上的<tbody>，把拼接好的字符串追加到里面
        document.querySelector('tbody').innerHTML = str
    }

    //跳转页面函数
    // function pageJump(url) {
    //     location.href = url
    // }

    //添加和修改用户,获取submit按钮dom对象，点击发送axios请求
    let submit_btn = document.querySelector(".submit")
    //给提交按钮绑定点击事件
    submit_btn.addEventListener('click',function (){
        //获取输入框的的值
        let id = document.querySelector("#id").value
        let username = document.querySelector("#usr").value
        let password = document.querySelector("#pwd").value
        let type = document.querySelector('input[name="opt-radio"]:checked').nextSibling.nodeValue
        //js的数据对象
        let data = {
            "id": id,
            "username": username,
            "password": password,
            "type": type
        }
        // 先判断id输入框的值是否为空,来区分添加和删除操作
        if(id === ''){
            //调用addUser()函数，发送axios请求
            addUser(data)
        }else {
            //调用updateUser()函数，发送axios请求
            updateUser(data)
        }

    })


    //删除和修改用户,使用事件冒泡,获取我点击了那个按钮
    let tbody = document.querySelector('tbody')
    tbody.addEventListener("click", function (e) {
        if(e.target.className === 'btn btn-danger'){
            //获取要删除用户的id,传回后端
            let id = e.target.dataset.id
            //调用删除用户函数
            deleteUser(id)
        } else {
            //获取要修改用户的id,传回后端
            let id = e.target.dataset.id
            //调用查询用户函数,传入想要修改数据的用户id
            getUserById(id)
        }
    })

    //点击搜索按钮,发起axios请求把搜索输入框的值传递给后端,再根据后端响应回来的数据重新渲染页面
    //1.先获取搜索按钮的dom对象
    let set_btn = document.querySelector(".set_btn")
    //2.给搜索按钮添加点击响应事件
    set_btn.addEventListener("click",function (){
        //3.获取搜索输入框的值
        let value = document.querySelector("#ret").value
        //4.调用搜索函数，传入value
        search(value)
    })

    //发起axios请求，用于搜索
    function search(value) {
        //请求地址
        let url = 'http://localhost:8080/my_demo/searchUser'
        //发送axios请求
        axios({
            method: "get",
            url: url,
            params:{
                value:value
            }
        }).then(res => {
            console.log(res)
            if (res.status === 200){
                let data = res.data
                //调用render函数,传入前端返回的数据
                render(data)
            }
        })
    }

</script>
</body>

</html>